local jiantong = fk.CreateSkill{
    name = "jin_heg__jiantong",
}

Fk:loadTranslationTable{
    ["jin_heg__jiantong"] = "监统",
    [":jin_heg__jiantong"] = "当你受到伤害后，你可观看一名角色所有手牌，然后你可用装备区内一张牌交换其中至多两张牌。",
    ["#jin_heg__jiantong-invoke"] = "监统：选择一名角色，观看其手牌",
    ["#jin_heg__jiantong-card"] = "监统：你可用一张装备牌交换其至多两张牌",
    ["#jin_heg__jiantong-method"] = "监统",

    ["$jin_heg__jiantong1"] = "秉公行事，无所亲疏。",
    ["$jin_heg__jiantong2"] = "明晰法理，通晓人情。",
}

local U = require "packages.utility.utility"

Fk:addPoxiMethod{
    name = "#jin_heg__jiantong-method",
    prompt = "#jin_heg__jiantong-card",
    card_filter = function(to_select, selected, data)
      if data and #selected < 3 then
        if table.contains(data[1][2], to_select) then
          local n = 0
          for _, id in ipairs(selected) do
            if table.contains(data[1][2], id) then
              n = n + 1
            end
          end
          return n < 2
        else
          local n = 0
          for _, id in ipairs(selected) do
            if table.contains(data[2][2], id) then
              n = n + 1
            end
          end
          return n < 1
        end
      end
    end,
    feasible = function (selected, data, extra_data)
      for _, id in ipairs(selected) do
        if table.contains(data[2][2], id) then
          return true
        end
      end
    end,
  }

jiantong:addEffect(fk.Damaged,{
    anim_type = "control",
    can_trigger = function(self, event, target, player, data)
        return player:hasSkill(jiantong.name) and target == player
      end,
      on_cost = function (self, event, target, player, data)
        local room = player.room
        local targets = table.filter(room.alive_players, function (p)
          return not p:isKongcheng()
        end)
        local tos = room:askToChoosePlayers(player,{
            targets = targets,
            min_num = 1,
            max_num = 1,
            prompt = "#jin_heg__jiantong-invoke",
            skill_name = jiantong.name
        })
        if #tos > 0 then
          event:setCostData(self,{tos = tos})
          return true
        end
      end,
      on_use = function(self, event, target, player, data)
        local room = player.room
        local toid = event:getCostData(self).tos[1]
        local to = toid
        local cards1 = to:getCardIds("h")
        local cards2 = player:getCardIds("e")
        local cards = room:askToPoxi(player,{
            poxi_type = "#jin_heg__jiantong-method",
            data = {
                { "$Hand", cards1 },
                { "$Equip", cards2 }
            }})
        if #cards == 0 then return end
        local mcards1 = table.filter(cards, function(id) return table.contains(cards1, id) end)
        local mcards2 = table.filter(cards, function(id) return table.contains(cards2, id) end)
        room:swapCards(player,{{player, mcards2},{to, mcards1}}, jiantong.name)
      end,
})

return jiantong